package top.yunmouren.areamusic.utils;


import top.yunmouren.areamusic.AreaMusic;
import top.yunmouren.areamusic.config.AreaDto;
import top.yunmouren.areamusic.config.MusicDto;
import top.yunmouren.areamusic.music.MusicTag;
import top.yunmouren.areamusic.player.PlayerBridge;

import java.util.*;

public class MusicUtil {
    private final AreaMusic plugin;

    public MusicUtil(AreaMusic plugin) {
        this.plugin = plugin;
    }

    protected void play(PlayerBridge playerBridge, AreaDto currentDto) {
        int playTarget = 0;
        if (currentDto.getRandom()) {
            int max = currentDto.getMusicId().size();
            if (max == 0) {
                return;
            }

            Random random = new Random();
            playTarget = random.nextInt(max);
        }

        String playTargetMusicId = currentDto.getMusicId().get(playTarget);
        MusicDto musicDto = this.plugin.getConfigManager().getMusics().get(playTargetMusicId);
        if (musicDto != null) {
            MusicTag musicTag = MusicTag.builder().musicId(musicDto.getMusicId()).musicUUID(musicDto.getUuid()).musicURL(musicDto.getMusicURL()).priority(currentDto.getPriority()).fromWorld(currentDto.getWorld()).areaUUID(currentDto.getUuid()).expiredTime(System.currentTimeMillis() + musicDto.getMusicDuration() * 1000L).build();
            playerBridge.setPlayerMusicTags(musicTag);
            playerBridge.getPlayer().playSound(playerBridge.getPlayer().getLocation(), musicTag.getMusicURL(), currentDto.getVolume(), currentDto.getPitch());
        }
    }

    protected void freshTag(PlayerBridge playerBridge) {
        MusicTag musicTag = playerBridge.getPlayerMusicTag();
        if (musicTag != null) {
            if (System.currentTimeMillis() > musicTag.getExpiredTime()) {
                playerBridge.clearPlayerMusicTag();
            }

        }
    }

    protected AreaDto selectMostImportantArea(List<AreaDto> areaDtos) {
        areaDtos.sort(Comparator.comparing((areaDtoFront) -> {
            return areaDtoFront.getPriority().getValue();
        }));
        return areaDtos.get(areaDtos.size() - 1);
    }

    protected List<AreaDto> getInsideList(String worldName, double x, double y, double z) {
        List<AreaDto> list = new ArrayList();
        if (this.plugin.getConfigManager().getAreas().get(worldName) != null) {
            (this.plugin.getConfigManager().getAreas().get(worldName)).forEach((key, value) -> {
                if (value.getLoop()) {
                    if (this.isInside(value, x, y, z)) {
                        list.add(value);
                    }

                }
            });
        }
        return list;
    }

    protected boolean isInside(AreaDto areaDto, double x, double y, double z) {
        AreaDto.Point minPoint = areaDto.getMinPoint();
        AreaDto.Point maxPoint = areaDto.getMaxPoint();
        return x >= minPoint.getX() && y >= minPoint.getY() && z >= minPoint.getZ() && x <= maxPoint.getX() + 1.0D && y <= maxPoint.getY() + 1.0D && z <= maxPoint.getZ() + 1.0D;
    }
}
